package com.yb0os1.serve.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yb0os1.context.BaseContext;
import com.yb0os1.dto.CategoryDTO;
import com.yb0os1.entity.Category;
import com.yb0os1.mapper.CategoryMapper;
import com.yb0os1.result.PageResult;
import com.yb0os1.serve.CategoryServe;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServeImpl implements CategoryServe {
    @Override
    public void deleteCategory(Long id) {
        categoryMapper.delete(id);
    }

    @Override
    public  List<Category>  getCategoryByType(Integer type) {
        return categoryMapper.select(null,type);
    }

    @Override
    public void updateCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
//        category.setUpdateTime(LocalDateTime.now());
//        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.update(category);
    }

    @Override
    public void isOpenCloseCategory(Long id, Integer status) {
        Category category = new Category();
        category.setId(id);
        category.setStatus(status);
//        category.setUpdateTime(LocalDateTime.now());
//        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.update(category);
    }

    @Override
    public PageResult pageSelect(String name, Integer page, Integer pageSize, Integer type) {
        PageHelper.startPage(page,pageSize);
        List<Category> categoryList =  categoryMapper.select(name,type);
        Page<Category>p = (Page<Category>) categoryList;
        return new PageResult(p.getTotal(),p.getResult());
    }

    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public void addCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
//        category.setCreateTime(LocalDateTime.now());
//        category.setUpdateTime(LocalDateTime.now());
//        category.setCreateUser(BaseContext.getCurrentId());
//        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.insert(category);
    }
}
